#include "system_setting.h"
#include <QtSql>
#define LABELWIDTH 100
#define LABELHIGHTH 30
#define AVGHEIGHT 30

System_Setting::System_Setting(QWidget *parent) :
    QWidget(parent)
{
    QFont ft;
    ft.setPixelSize(20);

    tabWidget=new QTabWidget();
    tabWidget->setFixedSize(900,450);
    QString tabBarStyle = "QTabBar::tab {min-width:100px;color:rgb(250,255,150);background-color:qlineargradient(x1:0, y1:0, x2:0, y2:1, stop: 0 rgb(180,183,188), stop: 1 gray);border: 0px solid;border-top-left-radius: 10px;border-top-right-radius: 10px;padding:5px;}QTabBar::tab:!selected {margin-top: 5px;}QTabBar::tab:selected {background-color:darkblue;color: yellow;}";
    tabWidget->setStyleSheet(tabBarStyle);
    tabWidget->setFont(ft);

    datebox=new QGroupBox(tr(""));
    userbox=new QWidget;
    userchange = new QWidget;
    resetbox=new QGroupBox(tr(""));




    time_setting();
    password_settring();
    sypassword_setting();
    QHBoxLayout *SLayout = new QHBoxLayout;
    SLayout->addSpacing(30);
    SLayout->addWidget(tabWidget);
    SLayout->addSpacing(30);

    QVBoxLayout *mainLayout = new QVBoxLayout;
    mainLayout->addLayout(SLayout);
    mainLayout->addStretch(1);
    setLayout(mainLayout);



//***************************************************************************//

    theKeyBoard = new Dialog(this);//虚拟键盘
    dateWidget = new QCalendarWidget(this);
    dateWidget->setFixedSize(370,210);
    dateWidget->hide();
    connect(dateWidget,SIGNAL(clicked(QDate)),this,SLOT(hideDateWidget(QDate)));
}

void System_Setting::change_passwd()
{
    QSqlQueryModel query;
    query.setQuery("SELECT * FROM password");
    int data = query.data(query.index(0,1)).toInt();
    qDebug()<<"data:"<<data<<query.rowCount();
    if(query.rowCount() == 0)
    {
        qDebug()<<"error";
        query.setQuery(QString("INSERT INTO password(user,passwd)""VALUES(1,88888888)"));
    }
    if ( password->text().toInt() == data)
    {
        userbox->hide();
        userchange->show();
        qDebug()<<"get in";
    }
    else
    {
        QMessageBox::information(NULL,tr("系统参数"),tr("密码错误，请重新输入！"));
        password->clear();
        return;
    }

}
void System_Setting::change_sypasswd()
{
    if (sypassword->text()=="88888888")
    {
        resetbox->hide();

    }
    else
    {
        QMessageBox::information(NULL,tr("系统参数"),tr("密码错误，请重新输入！"));
        sypassword->clear();
        return;
    }

}
bool System_Setting::eventFilter(QObject *obj, QEvent *event)
{
    if (obj == dayEdit)
    {
        if (event->type() == QEvent::MouseButtonRelease)
        {
            txtActived = dayEdit;
            showDateWidget();
            return true;
        }
        else
        {
            return false;
        }
    }
    if( obj == hour)
    {
        if(event->type() == QEvent::FocusIn)
        {
            theKeyBoard->setCurrentActive(hour);
        }
        else if(event->type() == QEvent::FocusOut)
        {
            theKeyBoard->hide();
        }
    }
    if( obj == min)
    {
        if(event->type() == QEvent::FocusIn)
        {
            theKeyBoard->setCurrentActive(min);
        }
        else if(event->type() == QEvent::FocusOut)
        {
            theKeyBoard->hide();
        }
    }
    if( obj == sec)
    {
        if(event->type() == QEvent::FocusIn)
        {
            theKeyBoard->setCurrentActive(sec);
        }
        else if(event->type() == QEvent::FocusOut)
        {
            theKeyBoard->hide();
        }
    }
    if( obj == password)
    {
        if(event->type() == QEvent::FocusIn)
        {
            theKeyBoard->setCurrentActive(password);
        }
        else if(event->type() == QEvent::FocusOut)
        {
            theKeyBoard->hide();
        }
    }
    if( obj == sypassword)
    {
        if(event->type() == QEvent::FocusIn)
        {
            theKeyBoard->setCurrentActive(sypassword);
        }
        else if(event->type() == QEvent::FocusOut)
        {
            theKeyBoard->hide();
        }
    }
    return false;
}

void System_Setting::showDateWidget()
{
    dateWidget->move(txtActived->pos().x()-30,txtActived->pos().y()+30);
    dateWidget->show();
}
void System_Setting::hideDateWidget(QDate date)
{
   // txtActived->setText(date.toString("yyyy-MM-dd"));

    char *name[]={"星期日","星期一","星期二","星期三","星期四","星期五","星期六"};
    txtActived->setText(date.toString("yyyy-MM-dd"));
   // weekEdit->setText(date.toString("dddd"));
    if(date.toString(("dddd"))=="Monday")
    {
        weekEdit->setText(name[1]);
    }
    if(date.toString(("dddd"))=="Tuesday")
    {
        weekEdit->setText(name[2]);
    }
    if(date.toString(("dddd"))=="Wednesday")
    {
        weekEdit->setText(name[3]);
    }
    if(date.toString(("dddd"))=="Thursday")
    {
        weekEdit->setText(name[4]);
    }
    if(date.toString(("dddd"))=="Friday")
    {
        weekEdit->setText(name[5]);
    }
    if(date.toString(("dddd"))=="Saturday")
    {
        weekEdit->setText(name[6]);
    }
    if(date.toString(("dddd"))=="Sunday")
    {
        weekEdit->setText(name[0]);
    }
    dateWidget->hide();
}


void System_Setting::recvResult(QByteArray rcvdByte)
{

}

void System_Setting::time_setting()
{
    //*****************************系统时间设定*************************************//
    QFont ft;
    ft.setPixelSize(20);
       QLabel *dateset = new QLabel(tr("系统日期设定:"));
       dateset->setFont(ft);

       QPushButton *ok = new QPushButton(tr("确认"));
       ok->setFont(ft);
       QLabel *week = new QLabel("星期:");
       week->setFont(ft);

       QLabel *time = new QLabel(tr("时间:"));
       time->setFont(ft);
      // ft.setPixelSize(15);
       dayEdit = new QLineEdit;
       dayEdit->setFixedSize(180,LABELHIGHTH-5);
       dayEdit->setFont(ft);
       dayEdit->setStyleSheet("QLineEdit{border-width:0;border-style:outset}");
       dayEdit->installEventFilter(this);


       weekEdit = new QLineEdit;
       weekEdit->setFont(ft);
       weekEdit->setFixedSize(100,LABELHIGHTH-5);
       weekEdit->setStyleSheet("QLineEdit{border-width:0;border-style:outset}");

       hour = new QLineEdit;
       hour->setFont(ft);
       hour->setFixedSize(50,LABELHIGHTH-5);
       hour->setStyleSheet("QLineEdit{border-width:0;border-style:outset}");
       hour->installEventFilter(this);

       min = new QLineEdit;
       min->setFont(ft);
       min->setFixedSize(50,LABELHIGHTH-5);
       min->setStyleSheet("QLineEdit{border-width:0;border-style:outset}");
       min->installEventFilter(this);

       sec = new QLineEdit;
       sec->setFont(ft);
       sec->setFixedSize(50,LABELHIGHTH-5);
       sec->setStyleSheet("QLineEdit{border-width:0;border-style:outset}");
       sec->installEventFilter(this);

       QLabel *xx=new QLabel(tr("："));
       xx->setFont(ft);
       QLabel *xx1=new QLabel(tr("："));
       xx1->setFont(ft);

       QHBoxLayout *date = new QHBoxLayout;
       //date->addStretch(1);
       date->addSpacing(50);
       date->addWidget(dateset);
       date->addSpacing(50);
       date->addWidget(dayEdit);
       date->addStretch(1);
       QHBoxLayout *date1 = new QHBoxLayout;
       date1->addSpacing(130);
       date1->addWidget(week);
       date1->addSpacing(50);
       date1->addWidget(weekEdit);
       date1->addStretch(1);

       QHBoxLayout *date2 = new QHBoxLayout;
       date2->addSpacing(130);
       date2->addWidget(time);
       date2->addSpacing(50);
       date2->addWidget(hour);
       date2->addWidget(xx);
       date2->addWidget(min);
       date2->addWidget(xx1);
       date2->addWidget(sec);
       date2->addStretch(1);

       QHBoxLayout *datebtn = new QHBoxLayout;
       datebtn->addStretch(1);
       datebtn->addWidget(ok);
       datebtn->addStretch(1);

       QVBoxLayout *maindata = new QVBoxLayout;
       //maindata->addStretch(1);
       maindata->addSpacing(20);
       maindata->addLayout(date);
       maindata->addSpacing(20);
       maindata->addLayout(date1);
       maindata->addSpacing(20);
       maindata->addLayout(date2);
       maindata->addSpacing(200);
       maindata->addLayout(datebtn);
       maindata->addStretch(1);

       datebox->setLayout(maindata);
       tabWidget->addTab(datebox,tr("日期时间设定"));
}

void System_Setting::password_settring()
{
    QFont ft;
    ft.setPixelSize(20);
    //*******************************用户密码修改***********************************//
        passwd = new QLabel(tr("原先密码输入:"));
        passwd->setFont(ft);
        password = new QLineEdit;
        password->setFont(ft);
        password->setEchoMode(QLineEdit::Password);
        password->installEventFilter(this);
        pdButton = new QPushButton(tr("确认"));
        pdButton->setFont(ft);
        QHBoxLayout *pdlayout1 = new QHBoxLayout;
        //pdlayout->addStretch(1);
        pdlayout1->addSpacing(40);
        pdlayout1->addWidget(passwd);
        pdlayout1->addWidget(password);
        pdlayout1->addStretch(1);

        QHBoxLayout *pdlayout2 = new QHBoxLayout;
        pdlayout2->addStretch(1);
        pdlayout2->addWidget(pdButton);
        pdlayout2->addStretch(1);
        QVBoxLayout *pdmain = new QVBoxLayout;
        pdmain->addSpacing(50);
        pdmain->addLayout(pdlayout1);
        pdmain->addSpacing(300);
        pdmain->addLayout(pdlayout2);
        //pdmain->addWidget(userchange);
        pdmain->addStretch(1);
        userbox->setLayout(pdmain);

        QLabel *newpd = new QLabel(tr("新密码输入:"));
        newpd->setFont(ft);
        newpdEdit = new QLineEdit;
        newpdEdit->setFont(ft);
        newpdEdit->setEchoMode(QLineEdit::Password);
        newpdEdit->installEventFilter(this);
        QPushButton *new_ok = new QPushButton(tr("确定"));
        new_ok->setFont(ft);
        QHBoxLayout *newlay = new QHBoxLayout;
        newlay->addSpacing(40);
        newlay->addWidget(newpd);
        newlay->addWidget(newpdEdit);
        newlay->addStretch(1);
        QHBoxLayout *newlay1 = new QHBoxLayout;
        newlay1->addStretch(1);
        newlay1->addWidget(new_ok);
        newlay1->addStretch(1);

        QVBoxLayout *newlay2 = new QVBoxLayout;
        newlay2->addSpacing(50);
        newlay2->addLayout(newlay);
        newlay2->addSpacing(300);
        newlay2->addLayout(newlay1);
        newlay2->addStretch(1);
        userchange->setLayout(newlay2);

        QVBoxLayout *main=new QVBoxLayout;
        //main->addStretch(1);
        main->addSpacing(50);
        main->addWidget(userbox);
        main->addWidget(userchange);
        main->addStretch(1);


        QGroupBox *maingroup = new QGroupBox;
        maingroup->setLayout(main);

        tabWidget->addTab(maingroup,tr("用户密码修改"));
        connect(pdButton,SIGNAL(clicked()),this,SLOT(change_passwd()));
        //userbox->hide();
        userchange->hide();
}

void System_Setting::sypassword_setting()
{
    QFont ft;
    ft.setPixelSize(20);
    //********************************系统初始化************************************//
        QLabel *sypasswd = new QLabel(tr("系统密码输入:"));
        sypasswd->setFont(ft);
        sypassword = new QLineEdit;
        sypassword->setFont(ft);
        sypassword->installEventFilter(this);
        sypassword->setEchoMode(QLineEdit::Password);
        QPushButton *sypdButton = new QPushButton(tr("确认"));
        sypdButton->setFont(ft);
        QHBoxLayout *sypdlayout1 = new QHBoxLayout;
        //pdlayout->addStretch(1);
        sypdlayout1->addSpacing(40);
        sypdlayout1->addWidget(sypasswd);
        sypdlayout1->addWidget(sypassword);
        sypdlayout1->addStretch(1);

        QHBoxLayout *sypdlayout2 = new QHBoxLayout;
        sypdlayout2->addStretch(1);
        sypdlayout2->addWidget(sypdButton);
        sypdlayout2->addStretch(1);

        QVBoxLayout *sypdmain = new QVBoxLayout;
        sypdmain->addSpacing(50);
        sypdmain->addLayout(sypdlayout1);
        sypdmain->addSpacing(300);
        sypdmain->addLayout(sypdlayout2);
        sypdmain->addStretch(1);

        resetbox->setLayout(sypdmain);
        tabWidget->addTab(resetbox,tr("系统初始化"));

        connect(sypdButton,SIGNAL(clicked()),this,SLOT(change_sypasswd()));
}
